package com.jianyun.jyzs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmSignCalendarActivity;
import com.jianyun.jyzs.bean.CrmSignDB;
import com.jianyun.jyzs.bean.DateInfo2;

import java.util.List;

/**
 * Created by mlli on 2017/12/1.
 */

public class CrmSignCalendarAdapter extends RecyclerView.Adapter {
    private CrmSignCalendarActivity context;
    private List<DateInfo2> list;

    @SuppressLint("WrongConstant")
    public CrmSignCalendarAdapter(CrmSignCalendarActivity context, List<DateInfo2> list) {
        context.getSystemService(Context.WINDOW_SERVICE);
        this.context = context;
        this.list = list;
    }

    public void setList(List<DateInfo2> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.wo_calendar_item, null);
        return new MyHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MyHolder myHolder = (MyHolder) holder;
        int screenWidth = getScreenWidth(context);
        myHolder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, screenWidth / 7));
        final DateInfo2 info = list.get(position);
        List<CrmSignDB> orders = info.getCrms();
        if (orders != null && orders.size() > 0) {
            int size = orders.size();
            myHolder.count.setText(size + "");
            myHolder.count.setVisibility(View.VISIBLE);
        } else {
            myHolder.count.setText("");
            myHolder.count.setVisibility(View.INVISIBLE);
        }
        myHolder.tv.setText(info.getDayOfM() + "");

        int type = info.getType();
        switch (type) {
            case 1:
                myHolder.tv.setTextColor(Color.GRAY);
                myHolder.itemView.setEnabled(false);
                break;
            case 2:
                myHolder.tv.setTextColor(Color.BLACK);
                myHolder.itemView.setEnabled(true);
                break;
            case 3:
                myHolder.tv.setTextColor(Color.GRAY);
                myHolder.itemView.setEnabled(false);
                break;
        }

        if (info.isToday()) {
            myHolder.tv.setBackgroundResource(R.drawable.calendar_today);
        } else {
            myHolder.tv.setBackgroundResource(0);
        }
        if (info.isSelected()){
            myHolder.tv.setBackgroundResource(R.drawable.calendar_selected);
        }
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                for (int i = 0; i < list.size(); i++) {
                    DateInfo2 dateInfo = list.get(i);
                    dateInfo.setSelected(false);
                }
                info.setSelected(true);
                notifyDataSetChanged();

                context.setOrderView(info.getCrms());

            }
        });
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private final TextView tv;
        private final TextView count;

        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.calendar_tv);
            count = (TextView) itemView.findViewById(R.id.count);
        }
    }

    @SuppressLint("WrongConstant")
    private int getScreenWidth(Context context) {
        return ((WindowManager) (context.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay().getWidth();
    }
}
